﻿namespace NOAH.CMS.Cache
{
    using System;
    using System.Web;
    using System.Web.Caching;

    public class BaseCacheLayer
    {
        protected virtual void AddCacheItem(string rawKey, object value)
        {
            Cache DataCache = HttpRuntime.Cache;
            if (DataCache[this.MasterCacheKeyArray()[0]] == null)
            {
                DataCache[this.MasterCacheKeyArray()[0]] = DateTime.Now;
            }
            CacheDependency dependency = new CacheDependency(null, this.MasterCacheKeyArray());
            DataCache.Insert(this.GetCacheKey(rawKey), value, dependency);
        }

        protected virtual object GetCacheItem(string rawKey)
        {
            return HttpRuntime.Cache[this.GetCacheKey(rawKey)];
        }

        protected virtual string GetCacheKey(string cacheKey)
        {
            return (this.MasterCacheKeyArray()[0] + "-" + cacheKey);
        }

        public virtual void InvalidateCache()
        {
            HttpRuntime.Cache.Remove(this.MasterCacheKeyArray()[0]);
        }

        protected virtual string[] MasterCacheKeyArray()
        {
            return new string[] { "MasterCacheKey" };
        }
    }
}

